﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Business.EntityFrameworkCore;
using mozhi.smarterp.Business.Purchases;
using mozhi.smarterp.Business.Purchases.Repositories;
using mozhi.smarterp.Business.Sales;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Business.Repositories.Purchase
{
    public class PurchaseProductPriceRepository : EfCoreRepository<BusinessDbContext, PurchaseProductPrice, Guid>, IPurchaseProductPriceRepository
    {
        public PurchaseProductPriceRepository(IDbContextProvider<BusinessDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<long> GetCountAsync(string keyword, string code, string name, Guid? supplierId, Guid? currencyId, Guid? excludeId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<PurchaseProductPrice>().AsNoTracking()
                .WhereIf(!keyword.IsNullOrEmpty() , x=>x.Code.Contains(keyword) || x.Name.Contains(keyword))
                .WhereIf(!code.IsNullOrEmpty() , x=>x.Code == code)
                .WhereIf(!name.IsNullOrEmpty(), x => x.Name == name)
                .WhereIf(supplierId.HasValue , x=>x.SupplierId == supplierId)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .WhereIf(excludeId.HasValue, x => x.Id!= excludeId)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<List<PurchaseProductPrice>> GetListAsync(Guid? supplierId, Guid currencyId, bool includeTax , DateTime date, List<Guid> productIds, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var productPrices = await dbContext.Set<PurchaseProductPrice>().AsNoTracking()
                .Where(x => x.CurrencyId == currencyId && x.IncludeTax == includeTax)
                .WhereIf(supplierId.HasValue, x => x.SupplierId == null || x.SupplierId == supplierId)
                .Include(x => x.Items.Where(y => productIds.Contains(y.ProductId) && y.EffectiveDate <= date && y.ExpirationDate >= date))
                .ToListAsync(cancellationToken);

            return productPrices;
        }

        public async Task<List<PurchaseProductPrice>> GetPagedListAsync(string keyword, string code, string name, Guid? supplierId, Guid? currencyId, int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = includeDetails ? (await WithDetailsAsync()).AsNoTracking() : dbContext.Set<PurchaseProductPrice>().AsNoTracking();

            var entities = await query
                .WhereIf(!keyword.IsNullOrEmpty(), x => x.Code.Contains(keyword) || x.Name.Contains(keyword))
                .WhereIf(!code.IsNullOrEmpty(), x => x.Code == code)
                .WhereIf(!name.IsNullOrEmpty(), x => x.Name == name)
                .WhereIf(supplierId.HasValue, x => x.SupplierId == supplierId)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .OrderByIf<PurchaseProductPrice, IQueryable<PurchaseProductPrice>>(!sorting.IsNullOrWhiteSpace(), sorting)
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(cancellationToken);

            return entities;
        }
    }
}
